#! /bin/sh
#
# $Id$
#

# install_prereq: a script to install distribution-specific
# prerequirements

set -e

usage() {
	echo "$0: a script to install distribution-specific prerequirement"
	echo 'Revision: $Id$'
	echo ""
	echo "Usage: $0:                    Shows this message."
	echo "Usage: $0 test                Prints commands it is about to run."
	echo "Usage: $0 install             Really install."
	echo "Usage: $0 install-unpackaged  Really install unpackaged requirements."
}

# Basic build system:
PACKAGES_DEBIAN="build-essential"
# Asterisk: basic requirements:
PACKAGES_DEBIAN="$PACKAGES_DEBIAN libedit-dev libjansson-dev libsqlite3-dev uuid-dev libxml2-dev"
# Asterisk: for addons:
PACKAGES_DEBIAN="$PACKAGES_DEBIAN libspeex-dev libspeexdsp-dev libogg-dev libvorbis-dev libasound2-dev portaudio19-dev libcurl4-openssl-dev xmlstarlet bison flex"
PACKAGES_DEBIAN="$PACKAGES_DEBIAN libpq-dev unixodbc-dev libltdl-dev libneon27-dev libgmime-2.6-dev liblua5.2-dev liburiparser-dev libxslt1-dev libssl-dev"
PACKAGES_DEBIAN="$PACKAGES_DEBIAN libh323plus-dev libvpb-dev libmysqlclient-dev libbluetooth-dev libradcli-dev freetds-dev libosptk-dev libjack-jackd2-dev bash"
PACKAGES_DEBIAN="$PACKAGES_DEBIAN libsnmp-dev libiksemel-dev libcorosync-common-dev libcpg-dev libcfg-dev libnewt-dev libpopt-dev libical-dev libspandsp-dev"
PACKAGES_DEBIAN="$PACKAGES_DEBIAN libresample1-dev libc-client2007e-dev binutils-dev libsrtp0-dev libsrtp2-dev libgsm1-dev doxygen graphviz zlib1g-dev libldap2-dev"
PACKAGES_DEBIAN="$PACKAGES_DEBIAN libcodec2-dev libfftw3-dev libsndfile1-dev libunbound-dev"
# Asterisk: for the unpackaged below:
PACKAGES_DEBIAN="$PACKAGES_DEBIAN wget subversion"
# Asterisk: for ./configure --with-pjproject-bundled:
PACKAGES_DEBIAN="$PACKAGES_DEBIAN bzip2 patch python-dev"

PACKAGES_RH="automake bzip2 gcc gcc-c++ patch ncurses-devel openssl-devel libxml2-devel unixODBC-devel libcurl-devel libogg-devel libvorbis-devel speex-devel"
PACKAGES_RH="$PACKAGES_RH spandsp-devel freetds-devel net-snmp-devel iksemel-devel corosynclib-devel newt-devel popt-devel libtool-ltdl-devel lua-devel"
PACKAGES_RH="$PACKAGES_RH sqlite-devel libsqlite3x-devel radiusclient-ng-devel portaudio-devel postgresql-devel libresample-devel neon-devel libical-devel"
PACKAGES_RH="$PACKAGES_RH openldap-devel gmime22-devel sqlite2-devel mysql-devel bluez-libs-devel jack-audio-connection-kit-devel gsm-devel libedit-devel libuuid-devel"
PACKAGES_RH="$PACKAGES_RH jansson-devel libsrtp-devel pjproject-devel subversion git libxslt-devel python-devel"

PACKAGES_OBSD="popt gmake wget libxml libogg libvorbis curl iksemel spandsp speex iodbc freetds-0.63p1-msdblib mysql-client gmime sqlite sqlite3 jack libxslt"
PACKAGES_FBSD="autoconf gcc binutils popt gmake wget libxml2 libogg libvorbis curl iksemel spandsp speex unixODBC freetds-devel mysql55-client gmime2 sqlite"
PACKAGES_FBSD="$PACKAGES_FBSD sqlite3 libxslt jansson e2fsprogs-libuuid gsm libsrtp libsamplerate"

KVERS=`uname -r`

JANSSON_VER=2.10

case "$1" in
test)
	testcmd=echo
	;;
install)
	testcmd=''
	;;
install-unpackaged)
	unpackaged="yes"
	;;
'')
	usage
	exit 0
	;;
*)
	usage
	exit 1
	;;
esac

in_test_mode() {
	test "$testcmd" != ''
}

check_installed_debs() {
	for pack in "$@" ; do
		tocheck="${tocheck} ^${pack}$ ~P^${pack}$"
	done
	pkgs=$(aptitude -F '%c %p' search ${tocheck} 2>/dev/null | awk '/^p/{print $2}')
	if [ ${#pkgs} -ne 0 ]; then
		echo $pkgs | sed -r -e "s/ ?[^ :]+:i386//g"
	fi
}

# parsing the output of yum is close to impossible.
# We'll use rpm and hope for the best:
check_installed_rpms() {
	for pack in "$@"
	do
		if ! rpm -q $pack >/dev/null 2>/dev/null
		then echo $pack
		fi
	done
}

check_installed_pkgs() {
	for pack in "$@"
	do
		if [ `pkg_info -a | grep $pack | wc -l` = 0 ]; then
		echo $pack
		fi
	done
}

check_installed_fpkgs() {
	for pack in "$@"
	do
		if [ `pkg info -a | grep $pack | wc -l` = 0 ]; then
		echo $pack
		fi
	done
}

handle_debian() {
	if ! [ -x "$(command -v aptitude)" ]; then
		apt-get install aptitude
	fi
	extra_packs=`check_installed_debs $PACKAGES_DEBIAN`
	$testcmd aptitude update
	if [ x"$extra_packs" != "x" ] ; then
		$testcmd aptitude install -y $extra_packs
	fi
}

handle_rh() {
	extra_packs=`check_installed_rpms $PACKAGES_RH`
	if [ x"$extra_packs" != "x" ] ; then
		$testcmd yum install --skip-broken --assumeyes $extra_packs
	fi
}

handle_obsd() {
	extra_packs=`check_installed_pkgs $PACKAGES_OBSD`
	if [ x"$extra_packs" != "x" ] ; then
		$testcmd pkg_add $extra_packs
	fi
}

handle_fbsd() {
	extra_packs=`check_installed_fpkgs $PACKAGES_FBSD`
	if [ x"$extra_packs" != "x" ] ; then
		$testcmd pkg install -y $extra_packs
	fi
}

install_unpackaged() {
	echo "*** Installing NBS (Network Broadcast Sound) ***"
	svn co http://svn.digium.com/svn/nbs/trunk nbs-trunk
	cd nbs-trunk
	make all install
	cd ..

	# Only install libresample if it wasn't installed via package
	if ! test -f /usr/include/libresample.h; then
		echo "*** Installing libresample ***"
		svn co http://svn.digium.com/svn/thirdparty/libresample/trunk libresample-trunk
		cd libresample-trunk
		./configure
		make all install
		cd ..
	fi

	# Only install Jansson if it wasn't installed via package
	if ! test -f /usr/include/jansson.h; then
		echo "*** Installing jansson ***"
		wget -O - http://www.digip.org/jansson/releases/jansson-${JANSSON_VER}.tar.gz | zcat | tar -xf -
		cd jansson-${JANSSON_VER}
		./configure
		make all install
		cd ..
		echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local.conf
		/sbin/ldconfig
	fi

	# Only install libsrtp2 if it wasn't installed via package
	if ! test -f /usr/include/srtp/srtp.h; then
		if ! test -f /usr/include/srtp2/srtp.h; then
			echo "*** Installing libsrtp2 ***"
			wget -O - http://github.com/cisco/libsrtp/archive/v2.tar.gz | zcat | tar -xf -
			cd libsrtp-2
			./configure --enable-openssl
			make shared_library install
			cd ..
			echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local.conf
			/sbin/ldconfig
		fi
	fi

	if ! test -f /usr/include/pjlib.h; then
		echo "PJProject not installed, yet. Therefore, please, run"
		echo "./configure --with-pjproject-bundled"
	fi
}

if in_test_mode; then
	echo "#############################################"
	echo "## $1: test mode."
	echo "## Use the commands here to install your system."
	echo "#############################################"
elif test "${unpackaged}" = "yes" ; then
	install_unpackaged
	exit 0
fi

OS=`uname -s`
unsupported_distro=''

# A number of distributions we don't (yet?) support.
if [ "$OS" != 'Linux' -a "$OS" != 'OpenBSD' -a "$OS" != 'FreeBSD' ]; then
	echo >&2 "$0: Your OS ($OS) is currently not supported. Aborting."
	exit 1
fi

if [ -f /etc/gentoo-release ]; then
	unsupported_distro='Gentoo'
fi

if [ -f /etc/mandrake-release ]; then
	unsupported_distro='Mandriva'
fi

if [ -f /etc/SuSE-release ]; then
	unsupported_distro='SUSE'
fi

if [ -f /etc/slackware-version ]; then
	unsupported_distro='Slackware'
fi

if [ "$unsupported_distro" != '' ]; then
	echo >&2 "$0: Your distribution ($unsupported_distro) is currently not supported. Aborting."
	exit 1
fi

# The distributions we do support:
if [ -r /etc/debian_version ]; then
	handle_debian
elif [ -r /etc/redhat-release ]; then
	handle_rh
elif [ "$OS" = 'OpenBSD' ]; then
	handle_obsd
elif [ "$OS" = 'FreeBSD' ]; then
	handle_fbsd
fi

if ! in_test_mode; then
	echo "#############################################"
	echo "## $1 completed successfully"
	echo "#############################################"
fi
